package com.ruoyi.test;

import java.util.*;

public class CustomerAndProduct {
    static Queue<Integer> list = new ArrayDeque<>();

    static Object luck = new Object();
    /**
     * 生产者线程
     */
    static Thread customerThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                Random random = new Random();
                int key = random.nextInt(10000);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (list.size() < 10) {
                    list.offer(key);
                    synchronized (list) {
                        list.notify();
                    }
                } else {
                    synchronized (list) {
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }
    });
    /**
     * 消费者线程
     */
    static Thread productThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                if (list.size() > 0) {
                    System.out.println(list.poll());
                    synchronized (list) {
                        list.notify();
                    }
                } else {
                    try {
                        synchronized (list) {
                            list.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    });

    public static void main(String[] args) {
//        customerThread.start();
//        productThread.start();
    }


}
